﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;

namespace Resto.CostControl.Web.Controllers
{
    public class CommonController : Controller
    {
        //
        // GET: /Common/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Export()
        {
            return View();
        }


        ///   <summary> 
        ///  获取某一日期是该年中的第几周
        ///   </summary> 
        ///   <param name="dt"> 日期 </param> 
        ///   <returns> 该日期在该年中的周数 </returns> 
        public static int GetWeekOfYear(DateTime dt)
        {
            GregorianCalendar gc = new GregorianCalendar(GregorianCalendarTypes.Localized);

            return gc.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
        }

        /// <summary>
        /// 获取某年某周的某天
        /// </summary>
        /// <param name="nweek"></param>
        /// <returns></returns>
        public static DateTime GetWeekDay(int year, int week, int day)
        {
            DateTime firstDate = new DateTime(year, 1, 1);//year为要求的那一年   
            int firstweekday = Convert.ToInt32(firstDate.DayOfWeek);//一年中第一天是周几   
            double days = (double)(7 - firstweekday);
            DateTime secondweekfisrtday = firstDate.AddDays(days); //第二周第一天
            DateTime fisrtday = secondweekfisrtday.AddDays((week - 2) * 7);//第N周第一天   
            DateTime getday = fisrtday.AddDays(day);
            return getday;
        }

        /// <summary>   
        /// 获取某一年有多少周   
        /// </summary>   
        /// <param name="year">年份</param>   
        /// <returns>该年周数</returns>   
        public static int GetWeekAmount(int year)
        {
            DateTime end = new DateTime(year, 12, 31);  //该年最后一天  
            
            return GetWeekOfYear(end);  //该年星期数   
        }
    }
}
